package com.loginlib.presenters;

import com.google.gson.Gson;
import com.wanhe.base.model.disgn.OrderedBill;
import com.wanhe.base.model.disgn.TableBillModel;
import com.wanhe.base.model.entity.TableBillEntity;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.presents.BasePresenter;
import com.wanhe.base.runner.Runner;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.callback.GetTableDetailCallBack;
import com.wanhe.base.websocket.callback.OpenTableCallBack;
import com.wanhe.base.websocket.model.response.BaseResponse;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2016-08-30.
 */
public class TableBillPresenter extends BasePresenter {

    private OnGetTableBillListener mOnGetTableBillListener;

    @Inject
    public TableBillPresenter(ServerApi api, Runner runner) {
        super(api, runner);
    }


    /**
     * 获取已选菜单列表
     *
     * @param posTableId
     * @param operId
     * @param password
     */
    public void getTableBill(String posTableId, String operId, String password) {
        runner.execute(api.getTableBill(posTableId, operId, password), new Consumer<TableBillEntity>() {
            @Override
            public void accept(@NonNull TableBillEntity tableBillEntity) throws Exception {
                if (ObjectUtil.checkObject(tableBillEntity)) {
                    if (tableBillEntity.resultType == TableBillEntity.SUCCESS) {

                        ArrayList<OrderedBill> arrOrderedBill = new ArrayList<>();
                        if (!ObjectUtil.checkObjectList(tableBillEntity.billDishes)) {
                            mOnGetTableBillListener.onGetTableBillFail("下单结果数据异常，请联系服务员");
                            return;
                        }
                        for (int i = 0; i < tableBillEntity.billDishes.size(); i++) {
                            TableBillModel model = tableBillEntity.billDishes.get(i);
                            OrderedBill orderedBill = new OrderedBill();
                            orderedBill.setTypeId(model.getCh_typeno());
                            orderedBill.setTypeName(model.getVch_typename());
                            if (arrOrderedBill.contains(orderedBill)) {
                                arrOrderedBill.get(arrOrderedBill.indexOf(orderedBill)).getArrTableBill().add(model);
                            } else {
                                orderedBill.getArrTableBill().add(model);
                                arrOrderedBill.add(orderedBill);
                            }
                        }
                        if(mOnGetTableBillListener!=null) mOnGetTableBillListener.onGetTableBillSuccess(arrOrderedBill);
                    } else {
                        if(mOnGetTableBillListener!=null)  mOnGetTableBillListener.onGetTableBillFail(tableBillEntity.resultMsg);
                    }
                } else {
                    if(mOnGetTableBillListener!=null)  mOnGetTableBillListener.onGetTableBillFail("已选菜单接口访问失败");
                }
            }
        });
    }
    public void getTableBill2(String posTableId){
        WebSocketTool.getInstance().getTableDetail(posTableId, new GetTableDetailCallBack() {
            @Override
            public void getTableDetailSuccess(String body) {
                Logger.d("getTableBill2=",body);
                Gson gson = new Gson();
                TableBillEntity tableBillEntity =   gson.fromJson(body, TableBillEntity.class);

                if (ObjectUtil.checkObject(tableBillEntity)) {
                    if (tableBillEntity.code == 0) {
                        ArrayList<OrderedBill> arrOrderedBill = new ArrayList<>();
                        if (!ObjectUtil.checkObjectList(tableBillEntity.billDishes)) {
                            mOnGetTableBillListener.onGetTableBillFail("下单结果数据异常，请联系服务员");
                            return;
                        }
                        for (int i = 0; i < tableBillEntity.billDishes.size(); i++) {
                            TableBillModel model = tableBillEntity.billDishes.get(i);
                            OrderedBill orderedBill = new OrderedBill();
                            orderedBill.setTypeId(model.getCh_typeno());
                            orderedBill.setTypeName(model.getVch_typename());
                            if (arrOrderedBill.contains(orderedBill)) {
                                arrOrderedBill.get(arrOrderedBill.indexOf(orderedBill)).getArrTableBill().add(model);
                            } else {
                                orderedBill.getArrTableBill().add(model);
                                arrOrderedBill.add(orderedBill);
                            }
                        }
                        if(mOnGetTableBillListener!=null) mOnGetTableBillListener.onGetTableBillSuccess(arrOrderedBill);
                    } else {
                        if(mOnGetTableBillListener!=null)  mOnGetTableBillListener.onGetTableBillFail(tableBillEntity.resultMsg);
                    }
                } else {
                    if(mOnGetTableBillListener!=null)  mOnGetTableBillListener.onGetTableBillFail("已选菜单接口访问失败");
                }
            }
        });
    }
    public void openTable(String guid, int num, OpenTableCallBack callBack){
        int tag =(int)System.currentTimeMillis();
        WebSocketTool.getInstance().openTable(tag,guid,num,callBack);
    }
//        JSONObject object = new JSONObject();
//        try {
//            object.put("paramJsonStr", "{\"op\":\"GetTableBill\",\"posTableId\":\"" + posTableId + "\",\"operId\":\"" + operId + "\",\"password\":\"" + password + "\"}");
//            Logger.i(TAG, "生成的json参数" + object.toString());
//            AppServerFactory.getFactory().getTableOperation().getTablesInfos(object.toString(), new BaseJsonResponseListener() {
//                @Override
//                public void onResponse(JSONObject response) {
//                    String result = response.toString();
//                    Logger.i(TAG, "已点菜单" + result);
//                    Gson gson = new Gson();
//                    TableBillEntity tableBillEntity = gson.fromJson(result, TableBillEntity.class);
//                    if (ObjectUtil.checkObject(tableBillEntity)) {
//
//                        if (tableBillEntity.resultType == TableBillEntity.SUCCESS) {
//
//                            ArrayList<OrderedBill> arrOrderedBill = new ArrayList<>();
//                            if (!ObjectUtil.checkObjectList(tableBillEntity.billDishes)){
//                                mOnGetTableBillListener.onGetTableBillFail("下单结果数据异常，请联系服务员");
//                                return;
//                            }
//                            for (int i = 0; i < tableBillEntity.billDishes.size(); i++) {
//                                TableBillModel model = tableBillEntity.billDishes.get(i);
//                                OrderedBill orderedBill = new OrderedBill();
//                                orderedBill.setTypeId(model.getCh_typeno());
//                                orderedBill.setTypeName(model.getVch_typename());
//                                if (arrOrderedBill.contains(orderedBill)) {
//                                    arrOrderedBill.get(arrOrderedBill.indexOf(orderedBill)).getArrTableBill().add(model);
//                                } else {
//                                    orderedBill.getArrTableBill().add(model);
//                                    arrOrderedBill.add(orderedBill);
//                                }
//                            }
//                            mOnGetTableBillListener.onGetTableBillSuccess(arrOrderedBill);
//
//
//                        } else {
//                            mOnGetTableBillListener.onGetTableBillFail(tableBillEntity.resultMsg);
//                        }
//                    } else {
//                        mOnGetTableBillListener.onGetTableBillFail("已选菜单接口访问失败");
//                    }
//                }
//            }, new BaseResponseErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError error) {
//                    mOnGetTableBillListener.onGetTableBillFail("网络错误");
//                }
//            });
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }

//    public void getDiscountType() {
//        JSONObject object = new JSONObject();
//        try {
//            object.put("paramJsonStr", "{\"op\":\"GetDiscountTypes\"}");
//            Logger.i(TAG, "生成的json参数" + object.toString());
//            AppServerFactory.getFactory().getTableOperation().getDiscountType(object.toString(), new BaseJsonResponseListener() {
//                @Override
//                public void onResponse(JSONObject response) {
//                    String result = response.toString();
//                    Logger.i(TAG, "优惠类型" + result);
//                    Gson gson = new Gson();
//                    DiscountTypeEntity typeEntity = gson.fromJson(result, DiscountTypeEntity.class);
//                    if (ObjectUtil.checkObject(typeEntity)) {
//
//                        if (typeEntity.resultType == BaseResultEntity.SUCCESS) {
//
//                            List<DiscountTypeEntity.DiscountTypeListBean> discountTypeList = typeEntity.getDiscountTypeList();
//                            if (ObjectUtil.checkObjectList(discountTypeList)) {
//                                if (checkRefrence(payBillViewWeakReference)) {
//                                    Map<String, String> discountTypes = new HashMap<>();
//                                    for (DiscountTypeEntity.DiscountTypeListBean bean : discountTypeList) {
//                                        String id = bean.getID();
//                                        String typename = bean.getName();
//                                        discountTypes.put(id, typename);
//                                    }
//                                    payBillViewWeakReference.get().discountType(discountTypes);
//                                }
//                            }
//
//                        } else {
//                            if (checkRefrence(payBillViewWeakReference)) {
//                                payBillViewWeakReference.get().onLoadFail("优惠类型接口访问失败");
//                            }
//                        }
//
//                    } else {
//                        if (checkRefrence(payBillViewWeakReference)) {
//                            payBillViewWeakReference.get().onLoadFail("优惠类型接口访问数据为空");
//                        }
//                    }
//                }
//            }, new BaseResponseErrorListener() {
//                @Override
//                public void onErrorResponse(VolleyError error) {
//                    if (checkRefrence(payBillViewWeakReference)) {
//                        payBillViewWeakReference.get().onLoadFail("接口访问超时，请联系柜台人员");
//                    }
//                }
//            });
//        } catch (JSONException e) {
//            Logger.e(TAG, e.getMessage());
//        }
//    }
//
//    @Override
//    public void onDestory() {
//        clearRefrence(payBillViewWeakReference);
//    }

    public void setOnGetTableBillListener(OnGetTableBillListener onGetTableBillListener) {
        this.mOnGetTableBillListener = onGetTableBillListener;
    }

    public interface OnGetTableBillListener {
        void onGetTableBillSuccess(ArrayList<OrderedBill> arrOrderedBill);

        void onGetTableBillFail(String msg);
    }


}
